package com.starblink.search.search.ui.cell

import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.ext.load
import com.starblink.basic.ext.screenWidth
import com.starblink.basic.style.view.round.CornerType
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.FetchSugWordQuery
import com.starblink.search.databinding.CellSearchSugBinding

class SearchSugCell(
    binding: CellSearchSugBinding,
    val itemClick: ((String, String?) -> Unit)? = null,
) :
    BaseBindingVH<FetchSugWordQuery.FetchSugWord, CellSearchSugBinding>(binding) {

    var curKeyword: String? = null

    private val contentW = screenWidth - itemView.paddingLeft - itemView.paddingRight

//    private val buttonGap = (binding.tvBtn1.layoutParams as ViewGroup.MarginLayoutParams?)?.rightMargin ?: 0

    private val dp16 = 16.dp2px()

    init {
        binding.tvText.minWidth = (screenWidth * 0.4f).toInt()
    }

    override fun onBind(model: FetchSugWordQuery.FetchSugWord, position: Int) {
        val word = model.sugWordVOF.word

        binding.clBg.trackData(
            element = SpmElementDef.SearchSugWord,
            page = SpmPageDef.SearchRoute,
            params = mutableMapOf(
                "searchTerm" to word,
                "index" to position.toString(),
                "type" to model.sugWordVOF.type.toString(),
            )
        )
        binding.tvText.trackData(
            element = SpmElementDef.SearchSugWord,
            page = SpmPageDef.SearchRoute,
            params = mutableMapOf(
                "searchTerm" to word,
                "index" to position.toString(),
                "type" to model.sugWordVOF.type.toString(),
            )
        )

        binding.clBg.setOnClickListener {
            itemClick?.invoke(word, null)
        }
        binding.tvText.setOnClickListener {
            itemClick?.invoke(word, null)
        }

        if (curKeyword.isNullOrEmpty()) {
            binding.tvText.text = word
        } else {
            val firstIndex = word.indexOf(curKeyword!!, ignoreCase = true)
            if (firstIndex > -1) {
                binding.tvText.buildSpannableString {
                    addText(word.substring(0, firstIndex)) {
                        setColor(color(com.starblink.basic.style.R.color.gu_text_primary))
                    }
                    val idxTag = firstIndex + curKeyword!!.length
                    addText(word.substring(firstIndex, idxTag)) {
                        setColor(color(com.starblink.basic.style.R.color.gu_text_brand600))
                    }
                    if (idxTag < word.length) {
                        addText(word.substring(idxTag, word.length)) {
                            setColor(color(com.starblink.basic.style.R.color.gu_text_primary))
                        }
                    }
                }
            } else {
                binding.tvText.text = word
            }
        }
        binding.ivType.load(model.sugWordVOF.iconLink)
        if (position == 0) {
            binding.clBg.setCornerRadius(CommUtils.dip2px(24f), CornerType.TOP_LEFT)
            binding.clBg.setCornerRadius(CommUtils.dip2px(24f), CornerType.TOP_RIGHT)
        } else {
            binding.clBg.setCornerRadius(
                0f,
                CornerType.ALL,
            )
        }

//        val textW = binding.tvText.paint.measureText(binding.tvText.text.toString())
//        val textLeftW = contentW - textW - dp16 - buttonGap
//        val btnList = model.sugWordVOF.buttonList
//        if (btnList.isNullOrEmpty()) {
//            binding.tvBtn1.gone()
//            binding.tvBtn2.gone()
//        } else {
//            var btn1W = 0f
//            if (btnList[0].isNotEmpty()) {
//                btn1W =
//                    binding.tvBtn1.paint.measureText(btnList[0]) + binding.tvBtn1.paddingLeft + binding.tvBtn1.paddingRight
//                binding.tvBtn1.visibleOrGone(textLeftW > btn1W) {
//                    text = btnList[0]
//                }
//            } else {
//                binding.tvBtn1.gone()
//            }
//            var visible2 = false
//            if (btnList.size > 1 && btnList[1].isNotEmpty()) {
//                val btn2W =
//                    binding.tvBtn2.paint.measureText(btnList[1]) + binding.tvBtn2.paddingLeft + binding.tvBtn2.paddingRight
//                visible2 = textLeftW > btn1W + btn2W
//                binding.tvBtn2.visibleOrGone(visible2) {
//                    text = btnList[1]
//                }
//            } else {
//                visible2 = false
//                binding.tvBtn2.gone()
//            }
//            binding.tvBtn1.takeIf { it.isVisible }?.let {
//                it.updateLayoutParams<ViewGroup.MarginLayoutParams> {
//                    rightMargin = if (visible2) buttonGap else 0
//                }
//            }
//        }
//        binding.tvBtn1.trackData(
//            element = SpmElementDef.SearchSugBotton,
//            page = SpmPageDef.SearchRecommendBody,
//            onlyHit=true,
//            params = mutableMapOf<String, String>().apply {
//                if(btnList!=null&& btnList.isNotEmpty()){
//                    put("searchTerm", btnList[0])
//                }
//            }
//        )
//        binding.tvBtn2.trackData(
//            element = SpmElementDef.SearchSugBotton,
//            page = SpmPageDef.SearchRecommendBody,
//            onlyHit=true,
//            params = mutableMapOf<String, String>().apply {
//                if(btnList!=null&& btnList.size>1){
//                    put("searchTerm", btnList[1])
//                }
//            }
//        )
//
//        binding.tvBtn1.setOnClickListener {
//            itemClick?.invoke(model.sugWordVOF.word, btnList?.get(0))
//        }
//        binding.tvBtn2.setOnClickListener {
//            itemClick?.invoke(model.sugWordVOF.word, btnList?.get(1))
//        }

    }
}